package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.common.Indirect;
import org.linlinjava.litemall.db.dao.LitemallUserMapper;
import org.linlinjava.litemall.db.dao.LookMoneyLogMapper;
import org.linlinjava.litemall.db.dao.YeJiMapper;
import org.linlinjava.litemall.db.domain.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class LitemallUserService {
    @Resource
    private LitemallUserMapper userMapper;


    @Autowired
    private LitemallMoneyLogService litemallMoneyLogService;

    @Autowired
    @Resource
    private YeJiMapper yeJiMapper;

    @Resource
    private LookMoneyLogMapper lookMoneyLogMapper;

    @Resource
    private Indirect indirect;



    public LitemallUser findById(Integer userId) {
        return userMapper.selectByPrimaryKey(userId);
    }

    public UserVo findUserVoById(Integer userId) {
        LitemallUser user = findById(userId);
        UserVo userVo = new UserVo();
        userVo.setNickname(user.getNickname());
        userVo.setAvatar(user.getAvatar());
        return userVo;
    }

    public LitemallUser queryByOid(String openId) {
        LitemallUserExample example = new LitemallUserExample();
        example.or().andWeixinOpenidEqualTo(openId).andDeletedEqualTo(false);
        return userMapper.selectOneByExample(example);
    }

    public void add(LitemallUser user) {
        user.setAddTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        userMapper.insertSelective(user);
    }

    public int updateById(LitemallUser user) {
        user.setUpdateTime(LocalDateTime.now());
        return userMapper.updateByPrimaryKeySelective(user);
    }

    public List<LitemallUser> querySelective(String username, String mobile, Integer page, Integer size, String sort, String order) {
        LitemallUserExample example = new LitemallUserExample();
        LitemallUserExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }

        if (!StringUtils.isEmpty(mobile)) {
            criteria.andMobileEqualTo(mobile);
        }
        criteria.andDeletedEqualTo(false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, size);
        return userMapper.selectByExample(example);
    }

    public int count() {
        LitemallUserExample example = new LitemallUserExample();
        example.or().andDeletedEqualTo(false);

        return (int) userMapper.countByExample(example);
    }

    public List<LitemallUser> queryByUsername(String username) {
        LitemallUserExample example = new LitemallUserExample();
        example.or().andUsernameEqualTo(username).andDeletedEqualTo(false);
        return userMapper.selectByExample(example);
    }

    public boolean checkByUsername(String username) {
        LitemallUserExample example = new LitemallUserExample();
        example.or().andUsernameEqualTo(username).andDeletedEqualTo(false);
        return userMapper.countByExample(example) != 0;
    }

    public List<LitemallUser> queryByMobile(String mobile) {
        LitemallUserExample example = new LitemallUserExample();
        example.or().andMobileEqualTo(mobile).andDeletedEqualTo(false);
        return userMapper.selectByExample(example);
    }

    public List<LitemallUser> queryByOpenid(String openid) {
        LitemallUserExample example = new LitemallUserExample();
        example.or().andWeixinOpenidEqualTo(openid).andDeletedEqualTo(false);
        return userMapper.selectByExample(example);
    }

    public void deleteById(Integer id) {
        userMapper.logicalDeleteByPrimaryKey(id);
    }


    /**
     * 这里没有用到
     *
     * @param id
     * @param xiaofeiquan
     * @return
     */
    public int addCrashByid(Integer id, Integer xiaofeiquan) {

        //1.根据id来更新账户余额

        return userMapper.addCrashByPrimaryKey(id, xiaofeiquan);
    }

    public List<LitemallUser> getUserInfoByQuery(String query) {

        return userMapper.getUserInfoByQuery(query);

    }

    public Integer getUserById(Integer id) {

        return userMapper.getUserById(id);
    }

    /*###################################################*/

    /**
     * * 通过手机号获取个人信息
     */
    public LitemallUser GetByUsermobile(String mobile) {
        return userMapper.GetByUsermobile(mobile);
    }

    ;

    //改用户 链
    public int updateUserlian(LitemallUser litemallUser) {
        return userMapper.updateUserlian(litemallUser);
    }

    /*##################################################*/


    /**************7月7日下午整合***************/

//改变钱
    public int updateUserMoney(LitemallUser litemallUser) {
        return userMapper.updateUserMoney(litemallUser);
    }

    //改变钱 ,减去钱
    public int updateUserMoneyj(LitemallUser litemallUser) {
        return userMapper.updateUserMoneyj(litemallUser);
    }

    //查链
    public String GetUserlian(int id) {
        return userMapper.GetUserlian(id);
    }

    /**
     * @param money 加钱
     * @param types 0，FSP   1 FST  2 SUN 3 SUND 4 MYPAY业绩   5KUISUN亏损  6奖励金
     * @return
     */
    public int MoneyProup(int id, double money, int types) {
        LitemallUser litemallUser = new LitemallUser();
        litemallUser.setId(id);
        if (types == 0) {
            litemallUser.setFSP(money);
        } else if (types == 1) {
            litemallUser.setFSPT(money);
        } else if (types == 2) {
            litemallUser.setSUN(money);
        } else if (types == 3) {
            litemallUser.setSUND(money);
        } else if (types == 4) {
            litemallUser.setMypay(money);
        } else if (types == 5) {
            litemallUser.setKuisun(money);
        } else if (types == 6) {
            litemallUser.setJianglijin(money);
        } else {
            return 0;
        }
        return userMapper.updateUserMoney(litemallUser);
    }

    /**
     * @param money 减钱
     * @param types 0，FSP   1 FST  2 SUN 3 SUND 4 MYPAY业绩   5KUISUN亏损  6奖励金
     * @return
     */
    public int MoneyProdown(int id, double money, int types) {
        LitemallUser litemallUser = new LitemallUser();
        litemallUser.setId(id);
        if (types == 0) {
            litemallUser.setFSP(money);
        } else if (types == 1) {
            litemallUser.setFSPT(money);
        } else if (types == 2) {
            litemallUser.setSUN(money);
        } else if (types == 3) {
            litemallUser.setSUND(money);
        } else if (types == 4) {
            litemallUser.setMypay(money);
        } else if (types == 5) {
            litemallUser.setKuisun(money);
        } else if (types == 6) {
            litemallUser.setJianglijin(money);
        } else {
            return 0;
        }
        return userMapper.updateUserMoneyj(litemallUser);
    }

    /**
     * 通过商品ID查询 该商品品牌 对应的用户
     *
     * @param id
     * @return
     */
    public LitemallUser selectBySPId(int id) {

        return userMapper.selectBySPId(id);

    }


    /**
     * 后台操作币，并且将记录存到数据库
     *
     * @param id
     * @param type
     * @param money
     * @param compute
     * @param remarks
     * @return
     */
    @Transactional
    public int updateFund(Integer id, String type, double money, Integer compute, String remarks, String admin) {

        LitemallPointsRecordService pointsRecordService = indirect.getPointsRecordService();

        //1.先通过用户id获取当前操作用户的资金信息
        LitemallUser user = userMapper.selectByPrimaryKeySelective(id);
        double integral = 0.0; // 积分

        //2.这个用户是用来更新用户资金信息的user
        LitemallUser updateUser = userMapper.selectByPrimaryKeySelective(id);
        updateUser.setId(id);


        //3.新建一个资金记录实体类来保存操作资金记录
//        LitemallMoneyLog litemallMoneyLog = new LitemallMoneyLog();
//        litemallMoneyLog.setType(type);
//        litemallMoneyLog.setRemarks(remarks);

        //  用积分表来代替上面的操
        // 更新积分
        //判断当前用户是增加操作还是减少操作，compute为0就是增加，compute为1就是减少
        if (compute == 0){
            pointsRecordService.setting(id,money); // 增加
            integral = user.getIntegral() + money;
        }else {
            pointsRecordService.setting(id,-money);// 减少
            integral = user.getIntegral() - money;
        }
        updateUser.setIntegral(integral);// 更新积分
        return userMapper.updateByPrimaryKeySelective(updateUser);


        //4.判断当前用户是增加操作还是减少操作，compute为0就是增加，compute为1就是减少
//        if (compute == 0) {
//
//            if (type.equals("FSP")) {
//                updateMoney = user.getFSP() + money;
//                updateUser.setFSP(updateMoney);
//                litemallMoneyLogService.insertMoneyByadmin(0, money, remarks, id, "FSP", admin, updateMoney);
//            }
//            if (type.equals("FST")) {
//                updateMoney = user.getFSPT() + money;
//                updateUser.setFSPT(updateMoney);
//                litemallMoneyLogService.insertMoneyByadmin(0, money, remarks, id, "FST", admin, updateMoney);
//            }
//            if (type.equals("SUN")) {
//                updateMoney = user.getSUN() + money;
//                updateUser.setSUN(updateMoney);
//                litemallMoneyLogService.insertMoneyByadmin(0, money, remarks, id, "SUN", admin, updateMoney);
//            }
//            if (type.equals("SUND")) {
//                updateMoney = user.getSUND() + money;
//                updateUser.setSUND(updateMoney);
//                litemallMoneyLogService.insertMoneyByadmin(0, money, remarks, id, "SUND", admin, updateMoney);
//            }
//            if (type.equals("消费券")) {
//                updateMoney = user.getXiaofeiquan() + money;
//                updateUser.setXiaofeiquan(updateMoney);
//                litemallMoneyLogService.insertMoneyByadmin(0, money, remarks, id, "消费券", admin, updateMoney);
//            }
//
//            if (type.equals("jianglijin")) {
//                updateMoney = user.getJianglijin() + money;
//                updateUser.setJianglijin(updateMoney);
//                litemallMoneyLogService.insertMoneyByadmin(0, money, remarks, id, "奖励金", admin, updateMoney);
//            }
//
//
//            return userMapper.updateByPrimaryKeySelective(updateUser);
//
//        } else if (compute == 1) {
//
//            if (type.equals("FSP")) {
//                if (user.getFSP() > money) {
//
//                    updateMoney = user.getFSP() - money;
//                    updateUser.setFSP(updateMoney);
//                    litemallMoneyLogService.insertMoneyByadmin(money, 0, remarks, id, "FSP", admin, updateMoney);
//                } else {
//                    return 0;
//                }
//            }
//            if (type.equals("FST")) {
//                if (user.getFSPT() > money) {
//                    updateMoney = user.getFSPT() - money;
//                    updateUser.setFSPT(updateMoney);
//                    litemallMoneyLogService.insertMoneyByadmin(money, 0, remarks, id, "FST", admin, updateMoney);
//                } else {
//                    return 0;
//                }
//            }
//
//            if (type.equals("SUN")) {
//                if (user.getSUN() > money) {
//                    updateMoney = user.getSUN() - money;
//                    updateUser.setSUN(updateMoney);
//                    litemallMoneyLogService.insertMoneyByadmin(money, 0, remarks, id, "SUN", admin, updateMoney);
//                } else {
//                    return 0;
//                }
//            }
//            if (type.equals("SUND")) {
//                if (user.getSUND() > money) {
//                    updateMoney = user.getSUND() - money;
//                    updateUser.setSUND(updateMoney);
//                    litemallMoneyLogService.insertMoneyByadmin(money, 0, remarks, id, "SUND", admin, updateMoney);
//                } else {
//                    return 0;
//                }
//            }
//            if (type.equals("消费券")) {
//                if (user.getXiaofeiquan() > money) {
//                    updateMoney = user.getXiaofeiquan() - money;
//                    updateUser.setXiaofeiquan(updateMoney);
//                    litemallMoneyLogService.insertMoneyByadmin(money, 0, remarks, id, "消费券", admin, updateMoney);
//                } else {
//                    return 0;
//                }
//            }
//
//
//            if (type.equals("奖励金")) {
//                if (user.getJianglijin() > money) {
//                    updateMoney = user.getJianglijin() - money;
//                    updateUser.setJianglijin(updateMoney);
//                    litemallMoneyLogService.insertMoneyByadmin(money, 0, remarks, id, "奖励金", admin, updateMoney);
//                } else {
//                    return 0;
//                }
//            }
//
//            return userMapper.updateByPrimaryKeySelective(updateUser);
//
//        } else {
//
//            return 0;
//        }


    }


    //根据id查找userlian里面包含id的用户数据
    public List<LitemallUser> findByUserlian(Integer userId) {

        return userMapper.findByUserlian(userId);
    }


    /**
     * 通过用户名获得 当前用户 团队业绩 团队人员  ，个人业绩
     *
     * @param id
     * @return
     */
    public YeJI getyejibyuserid(int id) {
        return yeJiMapper.selectbyid(id);
    }


    /**
     * \
     * 获得花钱日志记录
     *
     * @param userid
     * @return
     */
    public List<LookMoneyLog> selectMyrizhi(int userid) {

        return lookMoneyLogMapper.selectbyrq(userid);
    }

    /**
     * 当前所有的   查询所有人的fsp > 0
     *
     * @return
     */
    public List<LitemallUser> findfspdaling() {

        return userMapper.findfspdaling();

    }


    /**
     * 当前所有的   查询所有人的sun > 0
     *
     * @return
     */
    public List<LitemallUser> findsundaling() {

        return userMapper.findsundaling();

    }


    /**
     * @param money 加钱
     * @param types 0，FSP   1 FST  2 SUN 3 SUND 4 MYPAY业绩   5KUISUN亏损  6奖励金
     * @return
     */
    public int MoneyProMaXup(int id, double money, int types) {
        LitemallUser litemallUser = new LitemallUser();
        litemallUser.setId(id);
        if (types == 0) {
            litemallUser.setFSP(money);
        } else if (types == 1) {
            litemallUser.setFSPT(money);
        } else if (types == 2) {
            litemallUser.setSUN(money);
        } else if (types == 3) {
            litemallUser.setSUND(money);
        } else if (types == 4) {
            litemallUser.setMypay(money);
        } else if (types == 5) {
            litemallUser.setKuisun(money);
        } else if (types == 6) {
            litemallUser.setJianglijin(money);
        } else {
            return 0;
        }
        return userMapper.MoneyProMaXup(litemallUser);
    }


    public int jljjj(int id, double jianglijin) {

        LitemallUser litemallUser = new LitemallUser();
        litemallUser.setId(id);
        litemallUser.setJianglijin(jianglijin);
        return userMapper.jljjj(litemallUser);
    }

    public int modifyUser(Integer userId, LitemallUserVo userVo) {
        userVo.setId(userId);
        return userMapper.modify(userVo);
    }


    public     int payxiaofeiquan(int  id,double xfq){
        LitemallUser  litemallUser=new LitemallUser();
        litemallUser.setId(id);
        litemallUser.setXiaofeiquan(xfq);
        return userMapper.payxiaofeiquan(litemallUser);

    }

    /**
     * 获取支付密码
     *
     * @param id
     * @return
     */
    public  int  getpaypass(int  id){

return   userMapper.getPaypass(id);
    }

    public   int  setpaypass(int  id,  int  paypass){
        LitemallUser user=new LitemallUser();
        user.setId(id);
        user.setPaypassword(paypass);
        return   userMapper.updatepaypass(user);
    }

}
